# frozen_string_literal: true

module GeneralRuleSeed
  def self.run
    # 成年人， age > 0
    create_adult_rule

    create_teen_rule

    create_child_rule
  end

  def self.create_adult_rule
    rule = Rule::GeneralRule.create! name: 'adult rule'
    entity = rule.create_draft_entity!(
      type: 'RuleEntity::GeneralRule::Condition::All',
      stage: RuleEntity::Stage::DRAFT,
      status: RuleEntity::Status::CREATED,
      policy: RuleEntity::Policy::FIRST
    )
    entity.create_rule_schema! schema: age_schema

    entity.rule_conditions.create!(
      ordinal: 0,
      lvalue: 'age',
      operator: '>=',
      rvalue: 18
    )
    output_result = entity.rule_outputs.create!(
      name: 'result',
      type: 'RuleOutput::Dsl',
      ordinal: 0
    )
    Formula::Mrb.create!(
      formulable: output_result,
      content: 'age + 1'
    )
  end

  def self.create_teen_rule
    rule = Rule::GeneralRule.create! name: 'teen rule'
    entity = rule.create_draft_entity!(
      type: 'RuleEntity::GeneralRule::Condition::All',
      stage: RuleEntity::Stage::DRAFT,
      status: RuleEntity::Status::CREATED,
      policy: RuleEntity::Policy::FIRST
    )
    entity.create_rule_schema! schema: age_schema

    entity.rule_conditions.create!([
      {
        ordinal: 0,
        lvalue: 'age',
        operator: '>=',
        rvalue: 13
      },
      {
        ordinal: 1,
        lvalue: 'age',
        operator: '<',
        rvalue: 18
      }
    ])
    output_result = entity.rule_outputs.create!(
      name: 'result',
      type: 'RuleOutput::Dsl',
      ordinal: 0
    )
    Formula::Mrb.create!(
      formulable: output_result,
      content: 'age + 1'
    )
  end

  def self.create_child_rule
    rule = Rule::GeneralRule.create! name: 'child rule'
    entity = rule.create_draft_entity!(
      type: 'RuleEntity::GeneralRule::Condition::All',
      stage: RuleEntity::Stage::DRAFT,
      status: RuleEntity::Status::CREATED,
      policy: RuleEntity::Policy::FIRST
    )
    entity.create_rule_schema! schema: age_schema

    entity.rule_conditions.create!([
      {
        ordinal: 0,
        lvalue: 'age',
        operator: '>=',
        rvalue: 0
      },
      {
        ordinal: 1,
        lvalue: 'age',
        operator: '<',
        rvalue: 13
      }
    ])
    output_result = entity.rule_outputs.create!(
      name: 'result',
      type: 'RuleOutput::Dsl',
      ordinal: 0
    )
    Formula::Mrb.create!(
      formulable: output_result,
      content: 'age + 1'
    )
  end

  def self.age_schema
    SchemaSeed.age_schema
  end
end
